package com.wenlin.myproduction.widget;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;

import com.wenlin.myproduction.MyApplication;
import com.wenlin.myproduction.util.LogUtils;

import java.io.IOException;

public class PadMediaPlayer {

    private static PadMediaPlayer instance;

    public MediaPlayer getMediaPlayer() {
        return mMediaPlayer;
    }

    private MediaPlayer mMediaPlayer;
    private boolean isPrepared = false;//资源是否准备完成
    private boolean isPause;//是否暂停

    AudioManager mAudioManager;
    MediaPlayer.OnCompletionListener onCompletionListener;
    MediaPlayer.OnErrorListener mOnErrorListener;

    public synchronized static PadMediaPlayer getInstance() {
        if (instance == null) {
            instance = new PadMediaPlayer();
        }
        return instance;
    }

    public  static PadMediaPlayer create() {
        PadMediaPlayer player = new PadMediaPlayer();
        return player;
    }

    public void setSource(String source, boolean rigtNow) {
        reset();
        prepare(source, rigtNow);
    }

    public void setSource(Uri source, boolean rigtNow) {
        reset();
        prepare(source, rigtNow);
    }

    public void setSource(int source, boolean rigtNow) {
        reset();
        prepare(source, rigtNow);
    }

    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener) {
        initIfNeed(false);
        mMediaPlayer.setOnSeekCompleteListener(listener);
    }

    public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
        initIfNeed(false);
        this.onCompletionListener = listener;
        mMediaPlayer.setOnCompletionListener(listener);
    }
    public void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
        initIfNeed(false);
        this.mOnErrorListener = listener;
        mMediaPlayer.setOnErrorListener(listener);
    }

    MediaPlayer.OnPreparedListener onPreparedListener;

    public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
        onPreparedListener = listener;
        initIfNeed(false);

    }

    public void removeOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
        if (onPreparedListener == listener) {
            onPreparedListener = null;
        }
    }

    void reset() {
        isPrepared = false;
    }

    void initIfNeed(boolean rigtNow) {
        isPause = false;
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
        }
        if(onCompletionListener!=null){
            mMediaPlayer.setOnCompletionListener(onCompletionListener);
        }
        if(mOnErrorListener != null){
            mMediaPlayer.setOnErrorListener(mOnErrorListener);
        }

        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                isPrepared = true;
                if (rigtNow) {
                    if (!isPause) {
                        mediaPlayer.start();
                    }
                }
                if (onPreparedListener != null) {
                    onPreparedListener.onPrepared(mediaPlayer);
                }
            }
        });
    }

    void prepare(String source, boolean rigtNow) {
        if (TextUtils.isEmpty(source))
            return;
        initIfNeed(rigtNow);
        try {
            if (mMediaPlayer != null) {
                restSpeed();
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
                mMediaPlayer.reset();
            }
            LogUtils.d("source:" + source);
            mMediaPlayer.setDataSource(source);
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void prepare(Uri source, boolean rigtNow) {
        if (source == null)
            return;
        initIfNeed(rigtNow);
        try {
            if (mMediaPlayer != null) {
                restSpeed();
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
                mMediaPlayer.reset();
            }
            LogUtils.d("source:" + source);
            mMediaPlayer.setDataSource(MyApplication.getMyApplicationContext(), source);
            mMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void prepare(int source, boolean rigtNow) {
        if (source == 0)
            return;
        try {
            if (mMediaPlayer != null) {
                restSpeed();
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
                mMediaPlayer.reset();
            }
            isPause = false;
            mMediaPlayer = MediaPlayer.create(MyApplication.getMyApplicationContext(), source);
            if(onCompletionListener!=null){
                mMediaPlayer.setOnCompletionListener(onCompletionListener);
            }
            if(mOnErrorListener!=null){
                mMediaPlayer.setOnErrorListener(mOnErrorListener);
            }
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    isPrepared = true;
                    if (rigtNow) {
                        if (!isPause) {
                            mediaPlayer.start();
                        }
                    }
                    if (onPreparedListener != null) {
                        onPreparedListener.onPrepared(mediaPlayer);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void start() {
        isPause = false;
        if (isPrepared) {
            if (mMediaPlayer != null) {
                mMediaPlayer.start();
            }
        }
    }

    public void pause() {
        isPause = true;
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
        }
    }

    public boolean isPlaying() {
        return mMediaPlayer != null && mMediaPlayer.isPlaying();
    }

    public void release() {
        isPause = true;
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
        }
        mMediaPlayer = null;
    }

    public int getDuration() {
        try{
            return mMediaPlayer == null ? 0 :mMediaPlayer.getDuration();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    public int getCurrentPosition() {
        return mMediaPlayer == null ? 0 : mMediaPlayer.getCurrentPosition();
    }

    public void changePlayerSpeed(float speed) {
        try{
            if (mMediaPlayer != null) {
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.setPlaybackParams(mMediaPlayer.getPlaybackParams().setSpeed(speed));
                } else {//改变倍速会自动播放 所以暂停状态要重新暂停
                    mMediaPlayer.setPlaybackParams(mMediaPlayer.getPlaybackParams().setSpeed(speed));
                    mMediaPlayer.pause();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void restSpeed(){
//        try{
//            if (mMediaPlayer != null) {
//                float speed = mMediaPlayer.getPlaybackParams().getSpeed();
//                if(speed != 1.0f){
//                    if (mMediaPlayer.isPlaying()) {
//                        mMediaPlayer.setPlaybackParams(mMediaPlayer.getPlaybackParams().setSpeed(1.0f));
//                    } else {//改变倍速会自动播放 所以暂停状态要重新暂停
//                        mMediaPlayer.setPlaybackParams(mMediaPlayer.getPlaybackParams().setSpeed(1.0f));
//                        mMediaPlayer.pause();
//                    }
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }
    public void setLooping(boolean isLoop) {
        if (mMediaPlayer != null) {
            mMediaPlayer.setLooping(isLoop);
        }

    }

//    private void getStreamVolume() {
//        mAudioManager = (AudioManager) MyApplication.getMyApplicationContext().getSystemService(Context.AUDIO_SERVICE);
//
//        int min = mAudioManager.getStreamMinVolume(mAudioManager.STREAM_MUSIC);
//
//    }

    public void setVolume(float volume) {
//        if(mMediaPlayer!=null){
//            mMediaPlayer.setVolume(volume,volume);
//        }
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) MyApplication.getMyApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        }
        int max = mAudioManager.getStreamMaxVolume(mAudioManager.STREAM_MUSIC);
        int value = (int) (volume * max);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0); //音乐音量
    }

    public int getVolume() {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) MyApplication.getMyApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        }
        int max = mAudioManager.getStreamMaxVolume(mAudioManager.STREAM_MUSIC);
        int value = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        return value * 100 / max;
    }

    public void seekTo(int msec){
        if(isPrepared){
            try{
               if(mMediaPlayer!=null){
                   mMediaPlayer.seekTo(msec);
               }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
